package com.micolor.logr.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micolor.logr.demo.entity.UserCash;
import com.micolor.logr.demo.entity.UserInfo;
import com.micolor.logr.demo.mapper.UserCashMapper;
import com.micolor.logr.demo.service.IUserCashService;
import com.micolor.logr.runtime.annotation.LogRecord;
import com.micolor.logr.runtime.enums.OperateTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author EvanGe
 * @Type UserCashServiceImpl.java
 * @Desc
 * @date 2024/7/9 14:58
 */
@Service
@Slf4j
public class UserCashServiceImpl extends ServiceImpl<UserCashMapper, UserCash> implements IUserCashService {

    @Autowired
    UserCashMapper userCashMapper;
    @Override
    @LogRecord(description = "添加用户现金余额",topic = "cash_log_topic",entity = UserCash.class,operate = OperateTypeEnum.INSERT)
    public UserCash saveUserInfo(UserCash userCash) {
        userCashMapper.insert(userCash);
        return userCash;
    }

    @Override
    @LogRecord(description = "更新用户现金余额",topic = "cash_log_topic",entity = UserCash.class,operate = OperateTypeEnum.UPDATE)
    public UserCash updateUserInfo(UserCash oldUserCash, UserCash newUserCash) {
        userCashMapper.updateById(newUserCash);
        LambdaQueryWrapper<UserCash> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCash::getUserId,newUserCash.getUserId());
        return this.getOne(queryWrapper);
    }

    @Override
    @LogRecord(description = "删除用户现金余额",topic = "cash_log_topic",entity = UserCash.class,operate = OperateTypeEnum.DELETE)
    public UserCash deleteUserInfo(UserCash userCash) {
        userCashMapper.deleteById(userCash);
        return userCash;
    }
}
